package cn.anzhiyublog.staranimegallery.service.impl;

import cn.anzhiyublog.staranimegallery.mapper.TagMapper;
import cn.anzhiyublog.staranimegallery.model.domain.Tag;
import cn.anzhiyublog.staranimegallery.model.enums.StatusCode;
import cn.anzhiyublog.staranimegallery.service.TagService;
import cn.anzhiyublog.staranimegallery.util.ThrowUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 标签服务层
 *
 * @author 安知一只鱼
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Override
    public void add(String tagName) {
        Tag tag = new Tag();
        tag.setTagName(tagName);
        boolean isSave = this.save(tag);
        ThrowUtils.throwIf(!isSave, StatusCode.INTERNAL_SERVER_ERROR, "创建失败");
    }

    @Override
    public void delete(int id) {
        boolean isRemove = this.removeById(id);
        ThrowUtils.throwIf(!isRemove, StatusCode.INTERNAL_SERVER_ERROR, "删除失败");
    }

    @Override
    public void edit(int id, String tagName) {
        Tag tag = new Tag();
        tag.setId(id);
        tag.setTagName(tagName);
        boolean isUpdate = this.updateById(tag);
        ThrowUtils.throwIf(!isUpdate, StatusCode.INTERNAL_SERVER_ERROR, "修改失败");
    }

    @Override
    public List<Tag> listTag() {
        return this.list();
    }

    @Override
    public Tag getByName(String tagName) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getTagName, tagName);
        return this.baseMapper.selectOne(queryWrapper);
    }
}
